package com.uzletiwebtech.mancala.entity;

import java.util.ArrayList;
import java.util.List;

import com.uzletiwebtech.mancala.Constants;

public class PlayGround {

	//0. contains the jar
	private List<Integer> computerRow = new ArrayList<Integer>(7);
	//6. contains the jar
	private List<Integer> playerRow = new ArrayList<Integer>(7);
	
	private boolean playerTurn = false;

	public PlayGround() {
		computerRow.add(0);
		for (int i = 1; i <= Constants.TABLE_SIZE; i++) {
			if (i!=1) {
				computerRow.add(Constants.INITIAL_CELL_NUMBER);
			}
			if (i!=7) {
				playerRow.add(Constants.INITIAL_CELL_NUMBER);
			}
		}
		playerRow.add(0);
		playerTurn = true;
	}
	
	/**
	 * @return JSON formatted state of this playGround
	 */
	public String getStateInJSON() {
		StringBuilder state = new StringBuilder(100);
		state.append("{");
		
		//computer
		state.append("\"computer\":");
		state.append("{");
		state.append("\"jar\":").append(getComputerJar());
		state.append(",");
		state.append("\"row\":").append("[");
		for (int i = 1; i < computerRow.size(); i++) {
			state.append(computerRow.get(i));
			if (i!=6) {
				state.append(",");
			}
		}
		state.append("]");
		state.append("}");
		
		//player
		state.append(",");
		state.append("\"player\":");
		state.append("{");
		state.append("\"jar\":").append(getPlayerJar());
		state.append(",");
		state.append("\"row\":").append("[");
		for (int i = 0; i < playerRow.size()-1; i++) {
			state.append(playerRow.get(i));
			if (i!=5) {
				state.append(",");
			}
		}
		state.append("]");
		state.append("}");
		
		state.append("}");
		return state.toString();
	}
	
	public List<Integer> getComputerRow() {
		return computerRow;
	}

	public void setComputerRow(List<Integer> computerRow) {
		this.computerRow = computerRow;
	}

	public List<Integer> getPlayerRow() {
		return playerRow;
	}

	public void setPlayerRow(List<Integer> playerRow) {
		this.playerRow = playerRow;
	}

	public int getComputerJar() {
		return computerRow.get(0);
	}

	public void setComputerJar(int computerJar) {
		this.computerRow.set(0, computerJar);
	}

	public int getPlayerJar() {
		return playerRow.get(6);
	}

	public void setPlayerJar(int playerJar) {
		this.playerRow.set(6, playerJar);
	}
	
	public boolean isPlayerTurn() {
		return playerTurn;
	}

	public void setPlayerTurn(boolean playerTurn) {
		this.playerTurn = playerTurn;
	}
	
	@Override
	public String toString() {
		return getStateInJSON();
	}
}
